﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE;

namespace UI
{
	public partial class Cart : System.Web.UI.Page
	{
		protected void Page_Load( object sender, EventArgs e )
		{
            createListOfItems();
		}

        private void createListOfItems()
        {
            int id = 0;
            decimal fullPrice = 0;
            TableRow newRow;
            foreach (Album album in ShoppingCart.Instance.Cart)
            {
                newRow = new TableRow();
                tblCart.Controls.Add(newRow);

                LinkButton linkRemove = new LinkButton();
                linkRemove.Text = "Remove";
                linkRemove.CommandArgument = id.ToString();
                linkRemove.Command += CommandHandler;
                TableCell cell = new TableCell();
                cell.CssClass = "musicCell";
                cell.Controls.Add(linkRemove);

                newRow.Controls.Add(createStyledCell(album.Artist + " - " + album.Title));
                newRow.Controls.Add(createStyledCell(album.DecimalOutPut + " kr"));
                newRow.Controls.Add(cell);
                fullPrice += album.Price;
                id++;
            }
            tblCart.Controls.Add(newRow = new TableRow());
            newRow.Controls.Add(createCell("Full price: "));
            newRow.Controls.Add(createCell(fullPrice.ToString("0.00") + " kr And your balls"));

            LinkButton linkCheckout = new LinkButton();
            linkCheckout.Text = "Go to Checkout";
            linkCheckout.PostBackUrl = "Checkout.aspx";
            TableCell linkCell = new TableCell();
            linkCell.Controls.Add(linkCheckout);
            newRow.Controls.Add(linkCell);

        }

        private TableCell createStyledCell(string info)
        {
            TableCell cell = new TableCell();
            cell.Text = info;
            cell.CssClass = "musicCell";
            return cell;
        }

        private TableCell createCell(string info)
        {
            TableCell cell = new TableCell();
            cell.Text = info;
            return cell;
        }

        // bla
        protected void CommandHandler(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            ShoppingCart.Instance.removeItem(id);
            Master.UpdateCart();
            Response.Redirect("Cart.aspx");
        }
	}
}